package com.zxy.zxyrpc.server.tcp;

import com.zxy.zxyrpc.server.HttpServer;
import com.zxy.zxyrpc.server.VertxServer;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import io.vertx.core.parsetools.RecordParser;

/**
 * Vert 服务端
 *
 * @author zxy
 * @since 2025/5/22
 **/
public class VertxTcpServer implements VertxServer {
    public static void main(String[] args) {
        new VertxTcpServer().doStart(8888);
    }

    private byte[] handleRequest(byte[] requestData) {
        // todo 这里编写请求处理逻辑，根据 requestData 构造响应数据并返回
        return "Hello, Client".getBytes();
    }

    @Override
    public void doStart(int port) {
        // 创建vertx实例
        Vertx vertx = Vertx.vertx();
        // 创建TCP服务器
        NetServer server = vertx.createNetServer();
        // 处理请求
        /*server.connectHandler(socket -> {
            // 处理连接
            *//*socket.handler(buffer -> {
                // 接收字节数组数据，处理并响应
                *//**//*byte[] reqData = buffer.getBytes();
                byte[] resData = handleRequest(reqData);
                socket.write(Buffer.buffer(resData));*//**//*
                String textMessage = "Hello, server!Hello, server!Hello, server!";
                int messageLength = textMessage.getBytes().length;
                if (buffer.getBytes().length < messageLength) {
                    System.out.println("半包，length = " + buffer.getBytes().length);
                }
                if (buffer.getBytes().length > messageLength) {
                    System.out.println("粘包,length = " + buffer.getBytes().length);
                }
                System.out.println(buffer.getBytes().length);
                socket.write(Buffer.buffer("马马虎虎吧".getBytes()));
            });*//*
            RecordParser parser = RecordParser.newFixed(8);
            parser.setOutput(new Handler<Buffer>() {
                int size = -1;
                Buffer resultBuffer = Buffer.buffer();

                @Override
                public void handle(Buffer buffer) {
                    if (size == -1) {
                        // 消息体长度
                        size = buffer.getInt(4);
                        parser.fixedSizeMode(size);
                        // 写入头信息
                        resultBuffer.appendBuffer(buffer);
                    } else {
                        resultBuffer.appendBuffer(buffer);
                        System.out.println(resultBuffer.toString());
                        // 重置一轮
                        parser.fixedSizeMode(8);
                        size = -1;
                        resultBuffer = Buffer.buffer();
                    }
                }
            });
            socket.handler(parser);
        });*/

        server.connectHandler(new TcpServerHandler());
        // 启动 TCP 服务器并监听指定端口
        server.listen(port, result -> {
            if (result.succeeded())
                System.out.println("开启 TCP 服务在端口 " + port);
            else
                System.out.println("开启 TCP 服务端失败在端口 " + port);
        });
    }
}
